package com.example.user.controller.permission;

import cn.hutool.core.bean.BeanUtil;
import com.example.framework.common.pojo.R;
import com.example.user.controller.permission.vo.role.RoleRespVO;
import com.example.user.controller.permission.vo.role.RoleSaveReqVO;
import com.example.user.dal.dataobject.permission.RoleDO;
import com.example.user.service.permission.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @PostMapping("/create")
    @Operation(summary = "创建角色")
    @PreAuthorize("hasAuthority('system:role:create')")
    public R<Long> createRole(@Valid @RequestBody RoleSaveReqVO createReqVO) {
        return R.success(roleService.createRole(createReqVO, null));
    }

    @PutMapping("/update")
    @Operation(summary = "修改角色")
    @PreAuthorize("hasAuthority('system:role:update')")
    public R<Boolean> updateRole(@Valid @RequestBody RoleSaveReqVO updateReqVO) {
        roleService.updateRole(updateReqVO);
        return R.success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色")
    @Parameter(name = "id", description = "角色编号", required = true, example = "1024")
    @PreAuthorize("hasAuthority('system:role:delete')")
    public R<Boolean> deleteRole(@RequestParam("id") Long id) {
        roleService.deleteRole(id);
        return R.success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色信息")
    @PreAuthorize("hasAuthority('system:role:query')")
    public R<RoleRespVO> getRole(@RequestParam("id") Long id) {
        RoleDO role = roleService.getRole(id);
        return R.success(BeanUtil.toBean(role, RoleRespVO.class));
    }

}
